﻿using System;
using System.Diagnostics;
using System.Management;

namespace CCU
{
    internal class Utils
    {
        internal static string GetProcessUserName(int pID)
        {
            string text1 = null;

            SelectQuery query1 = new SelectQuery("Select * from Win32_Process WHERE processID=" + pID);
            ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(query1);

            try
            {
                foreach (ManagementObject disk in searcher1.Get())
                {
                    ManagementBaseObject inPar = null;
                    ManagementBaseObject outPar = null;

                    inPar = disk.GetMethodParameters("GetOwner");

                    outPar = disk.InvokeMethod("GetOwner", inPar, null);

                    text1 = outPar["User"].ToString();
                    break;
                }
            }
            catch
            {
                text1 = "SYSTEM";
            }
            return text1;
        }

        internal static string RunCmd(params string[] cmdLines)
        {
            return RunCmd(null, null, cmdLines);
        }

        internal static string RunCmd(string userName, string password, params string[] cmdLines)
        {
            string message = string.Empty;
            Process cmd = null;
            try
            {
                cmd = new Process();
                cmd.StartInfo.FileName = "cmd.exe";
                cmd.StartInfo.UseShellExecute = false;
                cmd.StartInfo.RedirectStandardInput = true;
                cmd.StartInfo.RedirectStandardOutput = true;
                cmd.StartInfo.RedirectStandardError = true;
                cmd.StartInfo.CreateNoWindow = true;
                if (!string.IsNullOrEmpty(userName))
                {
                    cmd.StartInfo.UserName = userName;
                }
                if (!string.IsNullOrEmpty(password))
                {
                    System.Security.SecureString ss = new System.Security.SecureString();
                    foreach (char c in password.ToCharArray())
                    {
                        ss.AppendChar(c);
                    }
                    cmd.StartInfo.Password = ss;
                }
                cmd.Start();
                foreach (string line in cmdLines)
                {
                    cmd.StandardInput.WriteLine(line);
                }
                cmd.StandardInput.WriteLine("exit");
                cmd.WaitForExit(60 * 1000);

                message = cmd.StandardOutput.ReadToEnd();
            }
            catch (Exception ex)
            {
                //throw ex;
                message = "Error:" + ex.Message;
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
            }

            return message;
        }
    }
}
